package game.players.states;

import game.ball.SoccerBall;
import game.players.SoccerPlayer;

import com.uqbar.vainilla.colissions.CollisionDetector;

public abstract class WithoutBall extends PlayerState {
	
	public WithoutBall(SoccerPlayer player) {
		super(player);
	}

	public void checkBallCollides(){
		SoccerPlayer player = this.getPlayer();
		SoccerBall ball = player.getBall();
		
		if(ball.isFree() && this.collidesAgainstBall(ball, this.getPlayer())) {
			double advanceX = player.getX() + player.getAppearance().getWidth() - ball.getX() - ball.getRatio();
			double advanceY = player.getY() + player.getAppearance().getHeight() - ball.getY() - ball.getRatio();
			
			ball.moveNormally(advanceX, advanceY);//necesario para evitar problemas con la camara
			
			this.changeBallPlayerStates(ball,player);
		}
	}
	
	@Override
	public boolean isWithBall() {
		return false;
	}
	
	public abstract void changeBallPlayerStates(SoccerBall ball,SoccerPlayer player);

	public boolean collidesAgainstBall(SoccerBall ball, SoccerPlayer player) {
		return CollisionDetector.INSTANCE.collidesCircleAgainstRect(
			ball.getX(), ball.getY(), ball.getRatio(),
			player.getX()+player.getAppearance().getWidth()-player.getFeetWidth(), 
			player.getY()+player.getAppearance().getHeight()-player.getFeetHeight(), 
			player.getFeetWidth(),player.getFeetHeight()
			);
	}
}
